﻿Public Class danhsachhangsx
    Private HangSanXuatBS As HangSanXuatBS
    Private HangSanXuatGD As HangSanXuatGD


    '------------ PageLoad

    Private Sub danhsachhangsx_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadDGV_DSHangSX()
        dgrvHangSX.Columns(0).HeaderText = "Mã Hãng SX"
        dgrvHangSX.Columns(1).HeaderText = "Tên Hãng SX"

        dgrvHangSX.SelectionMode = DataGridViewSelectionMode.FullRowSelect
    End Sub


    '------------ Load
    Private Sub LoadDGV_DSHangSX()

        Dim HangSanXuatList As New List(Of HangSanXuatGD)
        HangSanXuatBS = New HangSanXuatBS
        HangSanXuatList = HangSanXuatBS.GetData()
        dgrvHangSX.DataSource = HangSanXuatList
        dgrvHangSX.ClearSelection()
    End Sub

    Private Sub ClearTextBox()
        txtmahangsx.Text = ""
        txttenhangsx.Text = ""
    End Sub

    Private Sub ClearBinding()
        txtmahangsx.DataBindings.Clear()
        txttenhangsx.DataBindings.Clear()
    End Sub

    '-------- Các Button



    Private Sub btnthemhangsx_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthemhangsx.Click
        Me.GroupBox1.Visible = False
        Me.GroupBox2.Visible = True
        btncapnhathangsx.Visible = False

        ClearTextBox()
        ClearBinding()
    End Sub

    Private Sub btnsuahangsx_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsuahangsx.Click
        Me.GroupBox1.Visible = False
        Me.GroupBox2.Visible = True
        btnaddhangsx.Visible = False
        txtmahangsx.Enabled = False

        Dim maHangSX As String
        maHangSX = dgrvHangSX.SelectedRows.Item(0).Cells(0).Value
        Dim tenHangSX As String
        tenHangSX = dgrvHangSX.SelectedRows.Item(0).Cells(1).Value

        txtmahangsx.Text = maHangSX
        txttenhangsx.Text = tenHangSX
    End Sub

    Private Sub btnxoahangsx_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnxoahangsx.Click
        Dim maHangSX As String
        maHangSX = dgrvHangSX.SelectedRows.Item(0).Cells(0).Value
        Dim tenHangSX As String
        tenHangSX = dgrvHangSX.SelectedRows.Item(0).Cells(1).Value
        HangSanXuatGD = New HangSanXuatGD(maHangSX, tenHangSX)
        HangSanXuatBS.Delete(HangSanXuatGD)
        Me.GroupBox1.Visible = True
        Me.GroupBox2.Visible = False
        LoadDGV_DSHangSX()
    End Sub

    Private Sub btnthoathangsx_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthoathangsx.Click
        Me.Close()
    End Sub

    Private Sub btnaddhangsx_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnaddhangsx.Click
        Dim maHangSX As String = txtmahangsx.Text
        Dim tenHangSX As String = txttenhangsx.Text
        HangSanXuatGD = New HangSanXuatGD(maHangSX, tenHangSX)
        HangSanXuatBS.Insert(HangSanXuatGD)
        Me.GroupBox1.Visible = True
        Me.GroupBox2.Visible = False
        LoadDGV_DSHangSX()
        btncapnhathangsx.Visible = True
        btnaddhangsx.Visible = True
        btncapnhathangsx.Visible = True
    End Sub

    Private Sub btnbackhangsx_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnbackhangsx.Click
        ClearBinding()
        LoadDGV_DSHangSX()
        Me.GroupBox1.Visible = True
        Me.GroupBox2.Visible = False
        btncapnhathangsx.Visible = True
        btnaddhangsx.Visible = True
        txtmahangsx.Enabled = True
    End Sub

    

    Private Sub btncapnhathangsx_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncapnhathangsx.Click
        Dim maHangSX As String = txtmahangsx.Text
        Dim tenHangSX As String = txttenhangsx.Text
        HangSanXuatGD = New HangSanXuatGD(maHangSX, tenHangSX)
        HangSanXuatBS.Update(HangSanXuatGD)
        Me.GroupBox1.Visible = True
        Me.GroupBox2.Visible = False
        LoadDGV_DSHangSX()
        btncapnhathangsx.Visible = True
        btnaddhangsx.Visible = True
        txtmahangsx.Enabled = True

    End Sub

    Private Sub btntimhangsx_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btntimhangsx.Click
        dgrvHangSX.DataSource = HangSanXuatBS.GetByName(txttimhangsx.Text)
    End Sub

    '------ Các Sự Kiện

    
    Private Sub txttimhangsx_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles txttimhangsx.MouseClick
        radiotimkiem.Checked = True
    End Sub

    Private Sub radiotimkiem_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles radiotimkiem.MouseClick
        txttimhangsx.Focus()
    End Sub

    Private Sub radioselectall_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles radioselectall.MouseClick
        LoadDGV_DSHangSX()
    End Sub
End Class